package com.github.tyc.controller;

import cn.hutool.json.JSONUtil;
import com.github.tyc.entity.SysJob;
import com.github.tyc.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * 定时任务
 */
@RestController
@RequestMapping("/sys/schedule")
public class JobController {
	@Autowired
	private JobService scheduleJobService;
	
	/**
	 * 定时任务列表
	 */
	@RequestMapping("/list")
	public String list(@RequestParam Map<String, Object> params){
		return JSONUtil.toJsonStr(scheduleJobService.queryPage(params));
	}
	
	/**
	 * 定时任务信息
	 */
	@RequestMapping("/info/{jobId}")
	public String info(@PathVariable("jobId") Long jobId){
		SysJob schedule = scheduleJobService.getById(jobId);
		return JSONUtil.toJsonStr(schedule);
	}
	
	/**
	 * 保存定时任务
	 */
	@RequestMapping("/save")
	public String save(@RequestBody @Valid SysJob scheduleJob){
		scheduleJobService.saveJob(scheduleJob);
		return "OK";
	}
	
	/**
	 * 修改定时任务
	 */
	@RequestMapping("/update")
	public String update(@RequestBody SysJob scheduleJob){
		scheduleJobService.update(scheduleJob);
		return "OK";
	}
	
	/**
	 * 删除定时任务
	 */
	@RequestMapping("/delete")
	public String delete(@RequestBody Long[] jobIds){
		scheduleJobService.deleteBatch(jobIds);
		return "OK";
	}
	
	/**
	 * 立即执行任务
	 */
	@RequestMapping("/run")
	public String run(@RequestBody Long[] jobIds){
		scheduleJobService.run(jobIds);
		return "OK";
	}
	
	/**
	 * 暂停定时任务
	 */
	@RequestMapping("/pause")
	public String pause(@RequestBody Long[] jobIds){
		scheduleJobService.pause(jobIds);
		return "OK";
	}
	
	/**
	 * 恢复定时任务
	 */
	@RequestMapping("/resume")
	public String resume(@RequestBody Long[] jobIds){
		scheduleJobService.resume(jobIds);
		return "OK";
	}

}
